package com.shanjupay.transaction.controller;

import com.alipay.api.AlipayApiException;
import com.alipay.api.AlipayClient;
import com.alipay.api.DefaultAlipayClient;
import com.alipay.api.request.AlipayTradeWapPayRequest;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.GetMapping;

import javax.servlet.ServletException;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;

/**
 * @program: shanjupay
 * @description: 支付宝接口对接测试类
 * @author: Mr.Hkc
 * @create: 2020-08-22 09:12
 **/


@Controller
public class PayTestController {

    String APP_ID = "2021000117695424";
    String APP_PRIVATE_KEY = "MIIEvgIBADANBgkqhkiG9w0BAQEFAASCBKgwggSkAgEAAoIBAQCYNCu8jvVDBctm2GhuaNeiZ1t39Ce7BgI3aTQ/6kIh8zATJVIxiC5t5Fj/FqidK61R3bHLsvHl9eoZiBzR9PtX/ZPNeuDMR75qqUEAuaNAe6lyjO0JCkCYukfJejrx8I1V/qRr0AfSzYr40nd8iuPkRJgpV0P5GvzCvqBDk0Tiz4ezH+srtx+EWfDHVPRwRV7bAagc3eMMi9YARO+l0V1oBfumKRy+plgAehKFy6CqcpAW5R8qHW3HC+3/lOiLdi9NHygioBtZ8A8GvmPrcovj2f75BeuFY8sZRFKDI5WTSQlT56bSJj0LzcVzfYzEJmkSA267K1A+Vrql3mtbHrJXAgMBAAECggEBAIW4QDy3NI+fxaYjtwxZnCmYuOnYXheYHdZk3XzR6Sw+phwMC/xscq4lknL6i0UanHlyVsRqANBSj0vDI97qZvBVXseGj32qAodfEDb84YrDmomDerUYhswnzO4cLzFkcCf/QqFd8aGo59HXxxZqR/HCuhSIELYImczXAaKc/kZtRtQMYPzHd/LfaGZ0xdft/UV9aUWVYYr+IfXjAEYczrk7oly2Uf1YStgU0Y+d6btavx16tmJP1K8Hl6CyHafJ3459pTATbvEyAT7seuT4kpPJds0WPVXTrss0hskEamm8p8Uk4F2gYJOCTb3DfwXPdXyGxaqWD/Lp0CqFvQtXEgECgYEAz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R6+G9RUNgjBF/gliEy/PmiE4zSFSNaMkYPfzu6bOHNADdTX2Pmq4LYGeUox7FpCkXzOSiDOw7FHU+KGxqONt7MG/z2LpfyNpdU+g8Lvq7U/Gv46BEwzeIJv7rsXSlQVbEiPPziwesz/QxDH9AcUMdWVOlMOa3GxzJuuFoj7vq8Xlf8rkztwEbFfjuFzApZO2lXctlnxXGYQIDAQAB";
    String CHARSET = "utf-8";
    String serverUrl = "https://openapi.alipaydev.com/gateway.do";
    String sign_type = "RSA2";

    @GetMapping("/alipaytest")
    public void alipaytest(HttpServletRequest httpRequest,
                           HttpServletResponse httpResponse) throws ServletException, IOException {
        AlipayClient alipayClient = new DefaultAlipayClient(serverUrl, APP_ID, APP_PRIVATE_KEY, "json", CHARSET, ALIPAY_PUBLIC_KEY, sign_type); //获得初始化的AlipayClient
        AlipayTradeWapPayRequest alipayRequest = new AlipayTradeWapPayRequest();//创建API对应的request
//        alipayRequest.setReturnUrl("http://domain.com/CallBack/return_url.jsp");
//        alipayRequest.setNotifyUrl("http://domain.com/CallBack/notify_url.jsp");//在公共参数中设置回跳和通知地址
        alipayRequest.setBizContent("{" +
                " \"out_trade_no\":\"20200320010101002\"," +
                " \"total_amount\":\"88.88\"," +
                " \"subject\":\"Iphone6 16G\"," +
                " \"product_code\":\"QUICK_WAP_PAY\"" +
                " }");//填充业务参数
        String form = "";
        try {
            form = alipayClient.pageExecute(alipayRequest).getBody(); //调用SDK生成表单
        } catch (AlipayApiException e) {
            e.printStackTrace();
        }
        httpResponse.setContentType("text/html;charset=" + CHARSET);
        httpResponse.getWriter().write(form);//直接将完整的表单html输出到页面
        httpResponse.getWriter().flush();
        httpResponse.getWriter().close();
    }


}

